home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / octa209s.zip / octave-2.09 / src / dynamic-ld.cc < prev    next >
C/C++ Source or Header  |  1997-06-25  |  5KB  |  218 lines

  1. /*
  2.  
  3. Copyright (C) 1996 John W. Eaton
  4.  
  5. This file is part of Octave.
  6.  
  7. Octave is free software; you can redistribute it and/or modify it
  8. under the terms of the GNU General Public License as published by the
  9. Free Software Foundation; either version 2, or (at your option) any
  10. later version.
  11.  
  12. Octave is distributed in the hope that it will be useful, but WITHOUT
  13. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  15. for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with Octave; see the file COPYING.  If not, write to the Free
  19. Software Foundation, 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  20.  
  21. */
  22.  
  23. #ifdef HAVE_CONFIG_H
  24. #include <config.h>
  25. #endif
  26.  
  27. #if defined (WITH_SHL)
  28. #include <cerrno>
  29. #include <cstring>
  30. #endif
  31.  
  32. #include <strstream.h>
  33.  
  34. extern "C"
  35. {
  36. #if defined (WITH_DL)
  37. #include <dlfcn.h>
  38. #ifndef RTLD_LAZY
  39. #define RTLD_LAZY 1
  40. #endif
  41. #elif defined (WITH_SHL)
  42. #include <dl.h>
  43. #endif
  44. }
  45.  
  46. #include <defaults.h>
  47. #include "dirfns.h"
  48. #include "dynamic-ld.h"
  49. #include "error.h"
  50. #include "toplev.h"
  51. #include "pathsearch.h"
  52. #include "ov.h"
  53. #include "utils.h"
  54. #include "variables.h"
  55.  
  56. typedef builtin_function * (*Octave_builtin_fcn_struct_fcn)(void);
  57.  
  58. #if defined (WITH_DYNAMIC_LINKING)
  59.  
  60. // XXX FIXME XXX -- need to provide some way to ensure that functions
  61. // that we are going to use will use the same naming convention as
  62. // Octave's internal functions.  It needs to be simpler than the
  63. // current DEFUN_DLD() macro, which assumes you know how to name the
  64. // function, the struct, and the helper function.
  65.  
  66. static string
  67. mangle_octave_oct_file_name (const string& name)
  68. {
  69.   string retval ("FS");
  70.   retval.append (name);
  71.   retval.append ("__Fv");
  72.   return retval;
  73. }
  74.  
  75. #if defined (WITH_DL)
  76.  
  77. static void *
  78. dl_resolve_octave_reference (const string& name, const string& file)
  79. {
  80.   void *retval = 0;
  81.  
  82.   // Dynamic linking with dlopen/dlsym doesn't require specification
  83.   // of the libraries at runtime.  Instead, they are specified when
  84.   // the .oct file is created.
  85.  
  86.   void *handle = dlopen (file.c_str (), RTLD_LAZY);
  87.  
  88.   if (handle)
  89.     {
  90.       retval = dlsym (handle, name.c_str ());
  91.  
  92.       if (! retval)
  93.     {
  94.       const char *errmsg = dlerror ();
  95.  
  96.       if (errmsg)
  97.         error("%s: `%s'", name.c_str (), errmsg);
  98.       else
  99.         error("unable to link function `%s'", name.c_str ());
  100.  
  101.       dlclose (handle);
  102.     }
  103.     }
  104.   else
  105.     error ("%s: %s `%s'", dlerror (), file.c_str (), name.c_str ());
  106.  
  107.   return retval;
  108. }
  109.  
  110. #elif defined (WITH_SHL)
  111.  
  112. static void *
  113. shl_resolve_octave_reference (const string& name, const string& file)
  114. {
  115.   void *retval = 0;
  116.  
  117.   // Dynamic linking with shl_load/shl_findsym doesn't require
  118.   // specification of the libraries at runtime.  Instead, they are
  119.   // specified when the .oct file is created.
  120.  
  121.   shl_t handle = shl_load (file.c_str (), BIND_DEFERRED, 0L);
  122.  
  123.   const char *nm = name.c_str ();
  124.  
  125.   if (handle)
  126.     {
  127.       // Don't use TYPE_PROCEDURE here.  The man page says that future
  128.       // versions of HP-UX may not support it.
  129.  
  130.       int status = shl_findsym (&handle, nm, TYPE_UNDEFINED, &retval);
  131.  
  132.       if (status < 0)
  133.     {
  134.       const char *errmsg = strerror (errno);
  135.  
  136.       if (errmsg)
  137.         error("%s: `%s'", nm, errmsg);
  138.       else
  139.         error("unable to link function `%s'", nm);
  140.  
  141.       retval = 0;
  142.     }
  143.     }
  144.   else
  145.     error ("%s: %s `%s'", strerror (errno), file.c_str (), nm);
  146.  
  147.   return retval;
  148. }
  149.  
  150. #endif
  151. #endif
  152.  
  153. #if defined (WITH_DYNAMIC_LINKING)
  154. static void *
  155. resolve_octave_reference (const string& name, const string& file)
  156. {
  157. #if defined (WITH_DL)
  158.  
  159.   return dl_resolve_octave_reference (name, file);
  160.  
  161. #elif defined (WITH_SHL)
  162.  
  163.   return shl_resolve_octave_reference (name, file);
  164.  
  165. #endif
  166. }
  167. #endif
  168.  
  169. int
  170. load_octave_oct_file (const string& name)
  171. {
  172.   int retval = 0;
  173.  
  174. #if defined (WITH_DYNAMIC_LINKING)
  175.  
  176.   string oct_file = oct_file_in_path (name);
  177.  
  178.   if (! oct_file.empty ())
  179.     {
  180.       string mangled_name = mangle_octave_oct_file_name (name);
  181.  
  182.       Octave_builtin_fcn_struct_fcn f =
  183.     (Octave_builtin_fcn_struct_fcn) resolve_octave_reference
  184.       (mangled_name, oct_file);
  185.  
  186.       if (f)
  187.     {
  188.       builtin_function *s = f ();
  189.  
  190.       if (s)
  191.         {
  192.           install_builtin_function (*s);
  193.           retval = 1;
  194.         }
  195.     }
  196.     }
  197.  
  198. #else
  199.  
  200.   (void) name;
  201.  
  202. #endif
  203.  
  204.   return retval;
  205. }
  206.  
  207. void
  208. init_dynamic_linker (void)
  209. {
  210.   // Nothing to do anymore...
  211. }
  212.  
  213. /*
  214. ;;; Local Variables: ***
  215. ;;; mode: C++ ***
  216. ;;; End: ***
  217. */
  218.